import { decryptRes, dealPagination } from '../utils/requestUtil';
import * as activityService from '../services/activityService';
import { message } from 'antd';

export default {
	namespace: 'activity',

	state: {
		actListData: {},
		actDetail: {},
		pagination: {},
		baseInfo: {},

		returnData: {},
    actPagination:{},
		//tiemHistoryData:{},
	},

	effects: {
		*fetchActivityList({ payload }, { call, put }) {
			const response = JSON.parse(decryptRes(yield call(activityService.fetchActivityList, payload)));
			//const response = yield call(activityService.fetchActivityList, payload)
			console.log('接口返回数据：', response);
			if (response.code === 0) {
				let data = response.data;
				yield put({
					type: 'actListReducer',
					payload: { data },
				});
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
		},

		*upActList({ payload, callback }, { call, put }) {
			const response = JSON.parse(decryptRes(yield call(activityService.upActList, payload)));
			console.log(response);
			if (response.code === 0) {
				message.success('上架成功！');
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
			if (callback) callback();
		},

		*downActList({ payload, callback }, { call, put }) {
			console.log('前端参数', payload);
			const response = JSON.parse(decryptRes(yield call(activityService.downActList, payload)));
			console.log('接口返回数据：', response);

			if (response.code === 0) {
				message.success('下架成功！');
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
			if (callback) callback();
		},

		*delActList({ payload, callback }, { call, put }) {
			console.log('前端参数', payload);
			const response = JSON.parse(decryptRes(yield call(activityService.delActList, payload)));
			console.log('接口返回数据：', response);

			if (response.code === 0) {
				message.success('删除成功！');
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
			if (callback) callback();
		},

		*fetchActDetail({ payload, callback }, { call, put }) {
			const response = JSON.parse(decryptRes(yield call(activityService.fetchActDetail, payload)));
			console.log('活动详情----------', response);
			if (response.code === 0) {
				let data = response.data;
				yield put({
					type: 'actDetailReducer',
					payload: { data },
				});
				if (callback) callback(data);
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
		},

		*addImage({ payload, callback }, { call, put }) {
			console.log('前端参数', payload);
			const response = JSON.parse(decryptRes(yield call(activityService.addImage, payload)));
			console.log('接口返回数据：', response);

			if (response.code === 0) {
				let data = response.data;

				message.success('添加成功！');

				if (callback) callback(data.showImgUrl);
			} else {
				console.log(response.msg);
				message.error('调用服务器出错!' + response.msg);
			}
		},
		*editActivity({ payload, callback }, { call, put }) {
			console.log('前端参数', payload);
			const response = JSON.parse(decryptRes(yield call(activityService.editActivity, payload)));
			console.log('接口返回数据：', response);

			if (response.code === 0) {
				let data = response.data;

				message.success('修改成功！');
				yield put({
					type: 'editActReducer',
					payload: { data },
				});
				console.log('修改后返回的数据：', data);

				if (callback) callback(data);
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
		},

		*addActivity({ payload, callback }, { call, put }) {
			console.log('前端参数', payload);
			const response = JSON.parse(decryptRes(yield call(activityService.addActivity, payload)));
			console.log('接口返回数据：', response);

			if (response.code === 0) {
				let data = response.data;
				message.success('添加成功！');
				if (callback) callback(data);
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
		},
		*appointDay({ payload, callback }, { call, put }) {
			console.log('前端参数', payload);
			const response = JSON.parse(decryptRes(yield call(activityService.appointDay, payload)));
			console.log('接口返回数据：', response);

			if (response.code === 0) {
				let data = response.data;
				message.success('成功！');
				if (callback) callback();
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
		},
		*getUserActTimeHistory({ payload, callback }, { call, put }) {
			const response = JSON.parse(
				decryptRes(yield call(activityService.getUserActTimeHistory, payload))
			);
			console.log('获取指定时间段----------', response);
			if (response.code === 0) {
				let data = response.data;
				yield put({
					type: 'actTimeHistoryReducer',
					payload: { data },
				});
				if (callback) callback(data);
			} else {
				message.error('调用服务器出错!' + response.msg);
			}
		},
		*getActDateTimes({ payload, callback }, { call, put }) {
	      const response = JSON.parse(decryptRes(yield call(activityService.getActDateTimes, payload)));
	      if (response.code === 0) {
					let data = response.data;
					if (callback) callback(data);
				} else {
					message.error('调用服务器出错!' + response.msg);
				}
			},

		*actWriteOff({ payload, callback }, { call, put }) {
	      const response = JSON.parse(decryptRes(yield call(activityService.actWriteOff, payload)));
	      console.log(response)
	      if (response.code===0) {
				message.success('成功')
				} else {
					message.error('调用服务器出错!' + response.msg);
				}
			if (callback) callback();
			},
		},
		

	reducers: {
		actListReducer(state, action) {
			return {
				...state,
				actListData: action.payload.data,
				actPagination: {
          pageSize:action.payload.data.pageSize,
          pageNum:action.payload.data.pageNum,
          pages:action.payload.data.pages,
          size:action.payload.data.size,
          total:action.payload.data.total,
        }
			};
		},

		actDetailReducer(state, action) {
			console.log(action.payload.data);
			return {
				...state,
				actDetail: action.payload.data,
			};
		},
		editActReducer(state, action) {
			console.log(action.payload.data);
			return {
				...state,
				returnData: action.payload.data,
			};
		},
		actTimeHistoryReducer(state, action) {
			console.log(action.payload.data);
			return {
				...state,
				tiemHistoryData: action.payload.data,
			};
		},

	},
};
